﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace Inline.SharePoint.SPWebConfigCleaner
{

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        #region Events

        /// <summary>
        /// Connect to the SharePoint web application to get the registered web.config changes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {

            if (string.IsNullOrEmpty(tbSiteAddress.Text))
            {

                MessageBox.Show("Please provide the address of your SharePoint web application.");
                return;

            }

            try
            {

                using (SPSite site = new SPSite(tbSiteAddress.Text))
                {

                    SPWebApplication webApp = site.WebApplication;
                    if (webApp.WebConfigModifications.Count > 0)
                        FillListBoxWithAllSPWebConfigModifications(webApp.WebConfigModifications);
                    else
                        MessageBox.Show("No registered web.config changes could be found");

                }

            }
            catch(Exception ex)
            {

                MessageBox.Show("Could not connect to your SharePoint farm: " + ex.Message + ex.StackTrace);

            }

        }

        /// <summary>
        /// Remove the selected web.config changes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {

            if (lbConfigs.SelectedItems.Count == 0)
            {

                MessageBox.Show("Please select at least one entry.");
                return;

            }

            try
            {

                ClearConfigInformation();

                using (SPSite site = new SPSite(tbSiteAddress.Text))
                {

                    SPWebApplication webApp = site.WebApplication;

                    for (int i = 0; i < lbConfigs.SelectedItems.Count; i++)
                    {

                        for (int j = 0; j < webApp.WebConfigModifications.Count; j++)
                        {

                            Collection<SPWebConfigModification> oCollection = webApp.WebConfigModifications;

                            SPWebConfigModification oModification = webApp.WebConfigModifications[j];
                            if (oModification.Name == ((WebConfigListItem)lbConfigs.SelectedItems[i]).webConfigMod.Name)
                                oCollection.Remove(oModification);

                        }

                    }

                    webApp.Update();
                    webApp.WebService.ApplyWebConfigModifications();

                    FillListBoxWithAllSPWebConfigModifications(webApp.WebConfigModifications); // Refill listbox

                }

            }
            catch (Exception ex)
            {

                MessageBox.Show("Could not connect to your SharePoint farm: " + ex.Message + ex.StackTrace);

            }

        }

        /// <summary>
        /// Refresh the registered web.config changes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {

            if (string.IsNullOrEmpty(tbSiteAddress.Text))
            {

                MessageBox.Show("Please provide the address of your SharePoint farm.");
                return;

            }

            using (SPSite site = new SPSite(tbSiteAddress.Text))
            {

                SPWebApplication webApp = site.WebApplication;
                FillListBoxWithAllSPWebConfigModifications(webApp.WebConfigModifications); // Refill listbox

            }

        }

        /// <summary>
        /// If one modification is selected, show the information on the right
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbConfigs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            ClearConfigInformation();

            if (lbConfigs.SelectedItems.Count == 1)
            {

                WebConfigListItem webConfigListItem = (WebConfigListItem)lbConfigs.SelectedItem;

                txtConfigName.Text = webConfigListItem.webConfigMod.Name;
                txtConfigOwner.Text = webConfigListItem.webConfigMod.Owner;
                txtConfigPath.Text = webConfigListItem.webConfigMod.Path;
                txtConfigSequence.Text = webConfigListItem.webConfigMod.Sequence.ToString();
                txtConfigType.Text = webConfigListItem.webConfigMod.Type.ToString();
                txtConfigValue.Text = webConfigListItem.webConfigMod.Value;

            }

        }

        #endregion

        #region Helpers

        /// <summary>
        /// Fill the list box with the registered web.config changes
        /// </summary>
        /// <param name="webConfigMods"></param>
        private void FillListBoxWithAllSPWebConfigModifications(Collection<SPWebConfigModification> webConfigMods)
        {

            ClearConfigInformation(); // Reset
            lbConfigs.Items.Clear(); // Reset

            for (int i = 0; i < webConfigMods.Count; i++)
            {

                WebConfigListItem webConfigListItem = new WebConfigListItem();
                webConfigListItem.webConfigMod = webConfigMods[i];

                lbConfigs.Items.Add(webConfigListItem);

            }

        }

        /// <summary>
        /// Clear all the config information boxes
        /// </summary>
        private void ClearConfigInformation()
        {

            txtConfigName.Text = string.Empty;
            txtConfigOwner.Text = string.Empty;
            txtConfigPath.Text = string.Empty;
            txtConfigSequence.Text = string.Empty;
            txtConfigType.Text = string.Empty;
            txtConfigValue.Text = string.Empty;

        }

        #endregion

    }

}
